package com.okilo.online.app.act

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.okilo.online.app.R
import com.okilo.online.app.common.OkiloComAct
import com.okilo.online.app.configs.OkiloConfigs
import com.okilo.online.app.databinding.OkiloActPrivacyPolicyBinding
import com.okilo.online.app.tools.OkiloTitleBarUtil

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:17
 * @Annotation：WebView
 */
class OkiloPrivacyPolicyAct : OkiloComAct() {
    private lateinit var binding: OkiloActPrivacyPolicyBinding

    companion object {
        private const val INTENT_LINK_URL = "linkUrl"
        private const val INTENT_LINK_TITLE = "linkTitle"
        fun start(context: Context, linkTitle: String, linkUrl: String) {
            val intent = Intent(context, OkiloPrivacyPolicyAct::class.java)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            intent.putExtra(INTENT_LINK_TITLE, linkTitle)
            context.startActivity(intent)
        }
    }

    override val pageName: String = "PrivacyPolicy_Page"

    override fun createOkiloContentView(): View {
        binding = OkiloActPrivacyPolicyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initializeOkiloViews() {
        getTitleBarManager()
            .setTitleBarVisibility(View.VISIBLE)
            .setTitle(getWebViewTitle())
            .setTitleColor(getColor(R.color.okilo_color_black))
            .setRightButtonIcon(R.mipmap.okilo_root_customer_icon)
            .setBackButtonIcon(R.mipmap.okilo_root_back_icon)
            .setRootBackgroundColor(getColor(R.color.okilo_color_white))
        OkiloTitleBarUtil.setStatusBarLightMode(this, true)

        initWebViewSettings()
    }

    override fun setupOkiloEventListeners() {
        super.setupOkiloEventListeners()
    }

    override fun loadOkiloData() {
        binding.webView.loadUrl(getWebViewUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.webView.setInitialScale(100)
        binding.webView.webChromeClient = mWebChromeClient
        binding.webView.webViewClient = mWebClient
        val webSettings = binding.webView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.webLoading.progress = newProgress
            if (newProgress == 100) {
                binding.webLoading.visibility = View.GONE
            } else {
                binding.webLoading.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    private fun getWebViewUrl(): String {
        if (intent.extras == null){
            return OkiloConfigs.OKILO_PP_NIGERIA_LINK
        }
        return intent.extras?.getString(INTENT_LINK_URL).toString()
    }

    private fun getWebViewTitle(): String {
        if (intent.extras == null){
            return getString(R.string.okilo_privacy_policy)
        }
        return intent.extras?.getString(INTENT_LINK_TITLE).toString()
    }
}